#Set up google connection:
gs4_auth()
## ! Using an auto-discovered, cached token.
## To suppress this message, modify your code or options to clearly consent to
## the use of a cached token.
## See gargle's "Non-interactive auth" vignette for more details:
## <https://gargle.r-lib.org/articles/non-interactive-auth.html>
## ℹ The googlesheets4 package is using a cached token for 'grant@surefoot.me'.
#Declare sheet urls:
return_purchases_url <- "https://docs.google.com/spreadsheets/d/12qu9bQPdKA3IC1W9sYCqKr-M7F-1yeI62zcrX3MheUY/edit?gid=0#gid=0"
first_time_purchases_url <- "https://docs.google.com/spreadsheets/d/12qu9bQPdKA3IC1W9sYCqKr-M7F-1yeI62zcrX3MheUY/edit?gid=1689625029#gid=1689625029"
#Read in data:
return_purchases <- read_sheet(return_purchases_url, sheet = "Return Customer Purchases")
## ✔ Reading from "pdux-201 Repeat Purchase Rate".
## ✔ Range ''Return Customer Purchases''.
first_time_purchases <- read_sheet(first_time_purchases_url, sheet = "First-time Customer Purchases")
## ✔ Reading from "pdux-201 Repeat Purchase Rate".
## ✔ Range ''First-time Customer Purchases''.
#Make mutations to data:
return_purchases <- return_purchases %>% mutate(Status = "Returning")
first_time_purchases <- first_time_purchases %>% mutate(Status = "New")
#Merge data:
merged_purchases <- rbind(return_purchases, first_time_purchases) %>% mutate(
Year = as.numeric(substr(`Created At`, 1, 4)),
Week = as.numeric(substr(`Created At`, 11, 12)),
Date = as.Date(paste(Year, Week, 1, sep = "-"), "%Y-%U-%u"))
#Aggregated data:
aggregated_purchases <- merged_purchases %>%
group_by(Date, Status) %>%
summarize(
Orders = sum(`Total Orders`),
Refunds = sum(`Total Orders Refunded`),
.groups = 'drop'
)
#Create the visualizations
filled_purchase_plot <- ggplot(data = aggregated_purchases, aes(x = Date, y = Orders, fill = Status, group = Status)) +
geom_area(alpha = 0.5, aes(color = Status)) +
theme_minimal() +
labs(title = "Total Orders by Customer Status Over Time",
x = "Week Start",
y = "Number of Orders",
fill = "Customer Status",
color = "Customer Status") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
#Make visualizations interactive:
interactive_plot <- ggplotly(filled_purchase_plot)
interactive_plot <- interactive_plot %>%
layout(
xaxis = list(
rangeselector = list(
buttons = list(
list(count = 1,
label = "1m",
step = "month",
stepmode = "backward"),
list(count = 3,
label = "3m",
step = "month",
stepmode = "backward"),
list(count = 6,
label = "6m",
step = "month",
stepmode = "backward"),
list(step = "all")
)
),
rangeslider = list(type = "date")
)
)
interactive_plot